#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include "i2c_dev.h"

#define DEV_NAME "/dev/i2c-0"

int main(int argc, char **argv)
{
    printf("\n************************************************\n");
    int fd = 0;
    int i = 0;
    static I2cIoctlWrap ctrl = {0};
    static I2cMsgUser msgs[2] = {0};
    static unsigned char bufRead[10] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
    static unsigned char regBuf[4] = {0};
    fd = open(DEV_NAME, O_RDWR);
    if(fd == 0){
        printf("open %s failed error: %d\n",DEV_NAME, fd);
        return -1;
    }else{
        printf("open %s sucess fd: %d\n",DEV_NAME, fd);
    }
    regBuf[0] = 0x30 & 0xFF;
    ctrl.nmsgs = 2;
    ctrl.msgs = &msgs;
    /* 填充I2cMsg消息结构 */ 
    msgs[0].addr = 0x34;
    msgs[0].flags = 0; /* 标记为0，表示写入 */ 
    msgs[0].len = 1;
    msgs[0].buf = regBuf;

    msgs[1].addr = 0x34;
    msgs[1].flags = I2C_M_RD; /* 添加读标记位，表示读取 */ 
    msgs[1].len = 10;
    msgs[1].buf = &bufRead[0];

    ioctl(fd, IOCTL_RDWR, &ctrl);
    // sleep(5);
    close(fd);
    for (i = 0; i < 10; i++) {
        printf("bufRead[%d] = 0x%x\n", i, bufRead[i]);
    }

    printf("\n************************************************\n\n");
    
    return 0;
}